package zcjq.d831;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

// 最后一题
public class Box {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        // 数据组数
        int T = Integer.parseInt(scanner.nextLine());
        for (int i = 0; i < T; i++) { // for 每组数据
            int N = Integer.parseInt(scanner.nextLine());
            if (N == 1) {
                System.out.println(1);
                continue;
            }
            int[] nums = Arrays.stream(scanner.nextLine().trim().split(" ")).mapToInt(num -> Integer.parseInt(num)).toArray();
            List<Integer> numArray = new ArrayList<>();
            int total = 0;
            for (int num : nums) {
                if (num != 0) {
                    numArray.add(num);
                } else {
                    if (numArray.size() > 0) {
                        total += getCount(numArray);
                    }
                    numArray = new ArrayList<>();
                }
            }
            if (numArray.size() > 0) {
                total += getCount(numArray);
            }
            System.out.println(total);
        }
    }

    private static int getCount(List<Integer> nums) {
        int min = nums.stream().min((a, b) -> a - b).get();
        long count = nums.stream().filter(a -> a == min ? false : true).count();
        return Math.min((int) (min + count), nums.size());
    }

}
